Double-Caesar Cipher, Redux

Table of Contents

This is a “pair” assignment, which means that if you are working on a team with someone else, you and your partner should do your best to engage in the pair programming model. At any point in time, one of you is “driving,” i.e. actually using the keyboard and mouse. The other is actively engaged following along, preventing bugs, and providing ideas.

You should make sure that over the course of an assignment that you spend roughly the same amount of time each “driving.” I will also ask you to turn in a form rating the work that your partner does. My recommendation is to take turns approximately every 15 minutes or so. Set a timer to help you remember.

1. Overview

One of the unsatisfying things (from a software design perspective) of the last cipher assignment is that you had to break up all your code into multiple programs. It seems that encoding and decoding should all be part of the same program, where each task is separated into its own appropriate subsection. That’s what functions in Python are for, and here you’ll redo the previous assignment but create your own functions.

The key point of this assignment is to learn about writing functions. We’ll do so in the context of the Caesar cipher, but it’s the function structure that’s the main thing we’re after. To do this, you’ll need code from the last assignment. If you are working in a partnership, choose code from either one of you, it doesn’t matter which.

If neither you nor your partner managed to get successfully working cipher code and you wish to move on to achieve this assignment, let me know and I can supply you with a solution. Of course, that would mean that you cannot submit that for the previous assignment if you are still working on it.

2. Get started

Create a folder in which to store your work for this assignment.

  • If you are working on your own computer, it’s up to you where to put the folder. Your desktop is likely as good a place as any. Make a folder titled cipher2.
  • If you are working in the labs in Olin, make sure to first mount the COURSES folder, so that you won’t lose your code when you log out. Once you’ve done so, open up Finder, then navigate to your personal student work folder. You can then make a cipher2 folder within there.
  • Save the following three files into your cipher2 folder. The best way to do this is likely to right-click the links below, then select “save link as…” (or something similar, depending on which web browser you are using).
  • Once you’ve done so, you should then open up your new folder in VS Code. To do so, start up VS Code, then drag your folder onto the VS Code window. This should open up the folder within VS Code. If you are asked, click that you trust the authors.

3. Your assignment

Begin working in the file cipher.py that has been provided. Write three functions within:

  • encode(text, key): This function takes a string of text to be encoded as well as a text key, and returns the string in encoded form. You may assume that every character is a lower case letter. This function should not print anything.
  • decode(text, key): This function takes a string of encoded text and a text key, and returns the string in decoded form. This function should not print anything.
  • main(): This function drives the entire program. It should first ask the user if they want to encrypt or decrypt; if the user enters an e, it will encrypt; whereas a d will decrypt. The program will then take a message and a key, and then display the result.

Note that we are not actually grading your work in main(), so you could leave it out if you really wanted to, but you’ll probably be happier with the outcome if you use it. If you choose to complete main, you will need an extra if statement inside it, which would look something like

if response == "e":
    ...
else:
    ...

All code in your cipher.py file should exist inside of a function, with the exception of two lines at the bottom that will call the function main(). (I’ll provide this for you.)

> python cipher.py
Do you wish to (e)ncrypt or (d)ecrypt? e
What is the text? helloyou
What it the key? dog
The resulting text is ksroceri
> python cipher.py
Do you wish to (e)ncrypt or (d)ecrypt? d
What is the text? ksroceri
What it the key? dog
The resulting text is helloyou

You only need to add the code for decrypting if you want to go for the “Exemplary” grade. We’ll only test the encoding portion of the code for the “Meets Expectations” grade. (See more info about grading below.)

4. Automated tests

I will also be providing a Python program named tests_m.py, that will run a series of automated tests on the code that you write. (The “m” in the filename indicates that these are the tests for an M grade.) This is another one of the fantastic things about functions; other functions can call them in different ways. To run these tests, type pytest tests_m.py. You should receive an indication as to whether your code ran successfully or not. Hopefully, you installed pytest back a the beginning of the course when you got things set up, but if not, go back to week 1 of the course in Moodle, find the instructions for installing Python, and find the section involving pytest. It’s already installed in our labs, so you can work on those computers if you like.

5. Style

You should make sure that your program follows good style. You should it as readable as possible for someone else trying to understand them. At a minimum, you should put a comment at the top of each function explaining what the program does, and use comments above consecutive portions of Python code explaining what they do. You should also use meaningful and readable variable names. Furthermore, your code should be minimal where reasonable. In other words, your code shouldn’t contain an excessive number of variables or lines of code, unless doing so adds to readability.

6. Grading

You will receive an M for this assignment if…

  • when we run pytest tests_m.py, your program passes the tests.
  • your program is written to work in general, and not to only work for the specific tests that we have provided.

You will receive a grade of E for this assignment if you satisfy the above M requirements, and …

  • your programs have a comment at the top of each function with at least 5 words describing what the function does.
  • every one of your variable names is meaningful in some way. (Names such as thing, number, etc. are not meaningful.)
  • You have at least one other comment near what you think is the trickiest part of your code, describing how it works
  • when we run pytest tests_e.py, your program passes the tests.

7. Submit your work

When finished, zip up your code and submit your work through Moodle.

Good luck, and have fun! Remember that lab assistants are available to help out if you need it, and you can attend prefect sessions as well.

Author: Dave Musicant